<template>
	<view class="container">
		<view class="content-wrap">
			<view class="item" v-for="item in useDataResult.dataList">
				<text class="no">活动编号：{{ item.activityNo }}</text>
				<view class="line"></view>
				<text class="title">{{ item.activityName }}{{ item.list?.length > 1 ? '（卡包）' : '' }}</text>
				<view class="number">
					<text></text>
					<text v-if="item.type == 1">单客户可领：{{ item.numEveryone }}次</text>
					<text v-else>无限次使用二维码</text>
				</view>
				<view class="number">
					<text></text>
					<text v-if="item.type == 1">总共可领：{{ item.numTotal }}次</text>
				</view>
				<view class="number">
					<text class="price">¥ {{ couponPrice(item.list) }}</text>
					<text v-if="item.type == 1">已领：{{ item.numConsume }}次</text>
				</view>
				<view class="number">
					<text>券面额</text>
					<text v-if="item.type == 1">剩余：{{ (item.numTotal - item.numConsume) > 0 ? (item.numTotal - item.numConsume) : 0 }}次</text>
				</view>
				<view class="number">
					<view class="qrcode" @click="onOpenQr(item.qrcodeRecordId)">查看领券码</view>
					<view style="margin-top: 30rpx;">领券码归属：{{ item.tagInfo }}</view>
				</view>
			</view>
			<gzListNull :list="useDataResult.dataList"></gzListNull>
		</view>
		<!-- 二维码弹窗 -->
		<uni-popup ref="qrPopup" type="bottom">
			<view class="popup-qr">
				<text class="title">领券码查看</text>
				<uni-icons type="closeempty" size="24" color="darkgray" class="close" @click="onCloseQr()"></uni-icons>
				<view class="line"></view>
				<view class="content">
					<image v-if="vdata.qrImage" mode="widthFix" :src="vdata.qrImage"></image>
					<!-- <view class="des">无限次使用二维码</view> -->
				</view>
				<text class="bottom-wrap" @click="saveQRcode()">
					保存二维码
				</text>
			</view>
		</uni-popup>
	</view>
</template>

<script setup>
	import { reactive, ref, computed, getCurrentInstance } from 'vue'
	import { onLoad } from '@dcloudio/uni-app'
	import { $getQrcodeRecord, $getQrcode } from '@/http/marketing/apiMarketing.js'
	import useGetList from '@/hooks/useGetList.js'
	import storageManage from '@/util/storageManage.js'
	import numberUtil from '@/util/numberUtil.js'

	const { $infoBox } = getCurrentInstance().appContext.config.globalProperties

	const { useDataResult, getList } = useGetList($getQrcodeRecord, data => data, { mchNoList: storageManage.MCHS_CC().map(item => item.mchNo) })

	onLoad(({ activityId }) => {})

	// 二维码弹窗
	const qrPopup = ref();

	const vdata = reactive({
		qrImage: '',
	})
	// 卡券已派发数量
	const couponGet = computed(() => {
		return (couponList) => {
			if (couponList && couponList.length) {
				// return couponList.reduce((accumulator, currentValue) => accumulator + currentValue.couponGet, 0)
				return couponList[0].couponGet
			} else {
				return 0
			}
		}
	})
	// 卡券面额
	const couponPrice = computed(() => {
		return (couponList) => {
			if (couponList && couponList.length) {
				return numberUtil.getFloatNumber(couponList.reduce((accumulator, currentValue) => accumulator + currentValue.couponPrice, 0) / 100, 2)
			} else {
				return 0
			}
		}
	})

	/**
	 * 打开二维码弹窗
	 */
	const onOpenQr = (qrcodeRecordId) => {
		uni.showLoading({
			title: '加载中...'
		})
		$getQrcode({ qrcodeRecordId }).then(({ bizData }) => {
			vdata.qrImage = 'data:image/png;base64,' + bizData
			qrPopup.value.open()
			uni.hideLoading()
		})
	}

	/**
	 * 关闭二维码弹窗
	 */
	const onCloseQr = () => {
		qrPopup.value.close();
	}

	// 将base64 图片保存到本地系统相册
	function saveQRcode() {
		var writeContent = vdata.qrImage.replace('data:image/png;base64,', '')
		var fs = uni.getFileSystemManager();
		var fileName = 'IMG_' + new Date().getTime() + '.jpg'
		fs.writeFile({
			filePath: uni.env.USER_DATA_PATH + '/' + fileName,
			data: writeContent,
			encoding: 'base64',
			success: res => {
				uni.saveImageToPhotosAlbum({
					filePath: uni.env.USER_DATA_PATH + '/' + fileName,
					success: function(res) {
						$infoBox.toast('保存成功')
						onCloseQr()
					},
					fail: function(err) {
						// console.log(err)
					}
				})
				// console.log(res)
			},
			fail: err => {
				// console.log(err)
			}
		})
	}
</script>

<style lang="less">
	@page-backgroud: #F6F7FC; //页面背景色

	page {
		background: @page-backgroud;
	}

	.container {
		width: 100%;
		min-height: 100vh;
		padding: 30rpx 20rpx 30rpx 20rpx;
		box-sizing: border-box;
		overflow: hidden;

		.header-wrap {
			width: 100%;
			position: fixed;
			height: 120rpx;
			top: 0;
			left: 0;
			display: flex;
			justify-content: space-between;
			align-items: center;
			z-index: 2;
			box-sizing: border-box;
			padding: 0 30rpx;
			background-color: @page-backgroud;

			.title {
				font-weight: bold;
				font-size: 32rpx;
				color: #333333;
				font-style: normal;
				flex-grow: 1;
			}


			.filter {
				display: flex;
				align-items: center;
				border-radius: 40rpx;
				background-color: white;
				padding: 8rpx 20rpx;
				border: 1rpx solid #C8C8C8;

				text {
					font-size: 26rpx;
					color: black;
					letter-spacing: 1px;
					margin-bottom: 2rpx;
				}
			}
		}

		.content-wrap {
			width: 100%;
			position: relative;
			z-index: 0;

			.item {
				box-sizing: border-box;
				padding: 30rpx;
				background: #fff;
				border-radius: 20rpx;
				box-shadow: 0rpx -10rpx 30rpx -30rpx rgba(0, 0, 0, 0.8);
				display: flex;
				flex-direction: column;
				margin-bottom: 30rpx;


				.no {
					font-size: 26rpx;
					color: #333333;
				}

				.line {
					height: 1rpx;
					background-color: #E6E6E6;
					margin: 20rpx 0rpx;
				}

				.title {
					font-weight: bold;
					font-size: 32rpx;
					color: #333333;
					margin: 15rpx 0rpx;
				}

				.number {
					display: flex;
					align-items: center;
					flex-direction: row-reverse;
					justify-content: space-between;
					color: darkgray;
					font-size: 26rpx;
					margin: 5rpx 0rpx;

					.price {
						font-size: 38rpx;
						color: #FF8833;
						font-weight: bold;
					}

					.qrcode {
						border: 1rpx solid #C8C8C8;
						border-radius: 35rpx;
						background-color: white;
						padding: 15rpx 30rpx;
						color: #333333;
						margin-top: 30rpx;
						box-shadow: 0rpx -10rpx 30rpx -30rpx rgba(0, 0, 0, 0.8);
					}

				}

			}
		}

		.popup-content {
			display: flex;
			flex-direction: column;
			align-items: center;
			padding: 40rpx 30rpx;
			font-size: 26rpx;
			background-color: white;
			border-radius: 0 0 20rpx 20rpx;
			letter-spacing: 1px;
			box-sizing: border-box;
			margin-top: 120rpx;

			.item {
				display: flex;
				flex-direction: column;
				font-size: 28rpx;
				margin-bottom: 30rpx;
				width: 100%;

				.input {
					border-radius: 40rpx;
					padding: 18rpx 30rpx;
					background-color: #F6F7FC;
					margin-top: 20rpx;
					font-size: 30rpx;
				}

				.time {
					border-radius: 40rpx;
					padding: 18rpx 30rpx;
					background-color: #F6F7FC;
					font-size: 30rpx;
					flex-grow: 1;
					display: flex;
					justify-content: space-between;
					margin-top: 20rpx;
				}

			}


			.bottom-wrap {
				display: flex;
				flex-direction: row;
				align-items: center;
				font-size: 30rpx;
				margin-top: 30rpx;
				margin-bottom: 10rpx;
				width: 100%;

				.reset {
					color: black;
					border-radius: 40rpx;
					width: 25%;
					padding: 18rpx 0rpx;
					background-color: #F4F5F5;
					text-align: center;
					margin-right: 30rpx;
				}

				.query {
					color: white;
					border-radius: 40rpx;
					width: 75%;
					padding: 18rpx 0rpx;
					background-color: #24C789;
					margin-top: 30rpx;
					text-align: center;
					margin-bottom: 10rpx;
				}
			}

		}

		.popup-qr {
			display: flex;
			flex-direction: column;
			padding: 40rpx 30rpx;
			font-size: 28rpx;
			background-color: white;
			border-radius: 20rpx 20rpx 0 0;
			letter-spacing: 1px;
			box-sizing: border-box;
			align-items: center;
			position: relative;

			.title {
				font-weight: bold;
				font-size: 32rpx;
				color: #333333;
				margin-bottom: 20rpx;
				text-align: center;
			}

			.close {
				position: fixed;
				right: 0;
				top: 0;
				margin-top: 25rpx;
				margin-right: 25rpx;
			}

			.line {
				height: 1rpx;
				background-color: #E6E6E6;
				margin: 20rpx 0rpx;
				width: 100%;
			}



			.content {
				// border: 10rpx solid #24C78933;
				width: 70%;
				margin: 50rpx 0;
				text-align: center;
				// background-color: #24C78933;

				image {
					width: 100%;
					background-color: white;
					// border: 10rpx solid #24C78933;
					box-sizing: border-box;
				}

				.des {
					width: 100%;
					padding: 5rpx 0 15rpx 0rpx;
				}
			}

			.bottom-wrap {
				font-size: 30rpx;
				width: 100%;
				color: white;
				border-radius: 40rpx;
				padding: 20rpx 0rpx;
				background-color: #24C789;
				text-align: center;
				margin-bottom: 10rpx;
			}

		}

	}
</style>